<template>
  <div >

    <tableComponents
      :isHavePagination="false"
      :isShow="false"
      :isHaveExport="false"
      @toAdd="checkBd(true)"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list"
          v-loading="loading"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.9%"
        >
          <el-table-column type="selection" class="tc" width="35"></el-table-column>
          
          <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" width="80" min-width="80">
            <template slot-scope="scope">
              <el-input @change="sortinfo(scope.$index, scope.row)" v-model.number="scope.row.sort" autocomplete="off"
                size="small"></el-input>
            </template>
          </el-table-column>
          
          <el-table-column label="字段名称" min-width="80">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="英文标识" min-width="50">
            <template slot-scope="scope">{{ scope.row.iden }}</template>
          </el-table-column>

          <!-- <el-table-column label="表ID" min-width="40">
          <template slot-scope="scope">{{ scope.row.bid }}</template>
          </el-table-column>-->

          <el-table-column label="信息" min-width="160">
            <template slot-scope="scope">
              <div>
                <p>表单类型：{{ toTypes(scope.row.types) }}</p>
                <p>备注：{{ scope.row.remark || '---'}}</p>
                <p>默认文字：{{ scope.row.default|| '---' }}</p>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="参数" min-width="80">
            <template slot-scope="scope">{{ scope.row.parameter|| '---' }}</template>
          </el-table-column>

          <el-table-column prop="name" label="是否开启" min-width="80">
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.is_open"
                active-color="#1aad19"
                inactive-color="#ffffff"
                active-text="开启"
                inactive-text="关闭"
                active-value="1"
                @change="getOpen($event,scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="name" label="是否必填" min-width="80">
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.is_verification"
                active-color="#1aad19"
                inactive-color="#ffffff"
                active-text="是"
                inactive-text="不是"
                active-value="1"
                @change="getVerification($event,scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" min-width="120">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="checkBd(false,scope.row)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="deleteNew(scope.$index, scope.row.id)"
                type="danger"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>
    <el-dialog
      :close-on-click-modal="false"
      title="编辑"
      class="tl size_13"
      :append-to-body="true"
      :visible.sync="showBdFrom"
      width="800px"
    >
      <bd-field-form v-if="showBdFrom" @hideDio="getHideDio" :row="row" />
    </el-dialog>
  </div>
</template>
<script>
import HeadTitle from "@/components/currency/HeadTitle.vue";
import bdFieldForm from "./bd-field-form";
export default {
  data() {
    return {
      list: [],
      loading: true,
      showBdFrom: false,
      row: {}
    };
  },
  components: {
    HeadTitle,
    bdFieldForm
  },
  methods: {
    sortinfo(index, row) {
      // 排序
      var params = row;
      this.$api.getForm.saveField(params).then(res => {
        if (res.data.code == 200) {
          this.$message.success("修改成功");
          setTimeout(() => {
            this.getNewsAll();
          },1000)
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    sorta(a, b) {
      return b - a;
    },
    toTypes(type) {
      if (type == "1") {
        return "文本框";
      } else if (type == "2") {
        return "单选";
      } else if (type == "3") {
        return "多选";
      } else if (type == "4") {
        return "下拉";
      } else if (type == "5") {
        return "时间";
      } else if (type == "6") {
        return "图片";
      } else if (type == "7") {
        return "文本域";
      }
    },
    getHideDio(bool) {
      this.showBdFrom = false;
      if (bool) {
        this.getNewsAll();
      }
    },
    checkBd(bool, row) {
      if (bool) {
        this.row = {};
      } else {
        this.row = row;
      }
      this.showBdFrom = true;
    },
    getOpen(val, row) {
      var params = {};
      params = row;
      params.is_open = val;
      this.updata(params);
    },
    getVerification(val, row) {
      var params = {};
      params = row;
      params.is_verification = val;
      this.updata(params);
    },
    updata(row, bool) {
      var params = {};
      params = row;
      this.$api.getForm.saveField(params).then(res => {
        if (res.data.status == 1 && res.data.code == 200) {
          if (bool) {
            this.$message.success("提交成功");
          }
          //  this.$message.success('提交成功');
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    getNewsAll() {
      var id = this.$route.query.id || "";
      this.$api.getForm.getFieldList({ id }).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
          // console.log(this.list)
        }
        this.loading = false;
      });
    },
    deleteNew(index, id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id: id };
          this.$api.getForm.delField(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.list.splice(index, 1);
            } else {
              this.$swal("删除失败！", res.data.result, "success");
            }
          });
        }
      });
    }
  },

  created() {
    this.getNewsAll();
  },
  computed: {}
};
</script>

<style lang="less" scoped>
thead > tr > th {
  height: 45px;
  line-height: 45px;
}
td,
th {
  border: 1px solid #e1e3e4;
  font-size: 12px;
}
th {
  background: #f8f8f8;
  font-weight: normal;
  color: #323437;
}
td {
  height: 75px;
  color: #000;
}
.caozuo {
  padding-left: 50px;
}
.pl {
  padding-left: 15px;
}
.chkBox {
  display: block;
  margin: 0 auto;
  width: 18px;
  height: 18px;
  background: #fff;
}
.aBtn_1 {
  padding: 10px;
}
</style>
